package com.cssw.fyzb.core.dao.system;

import com.cssw.fyzb.core.model.dodb.SysOper;
import com.cssw.fyzb.core.model.dodb.SysOperRole;
import com.ydn.dbframe.plugin.activerecord.Db;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * @Author: YangYiQun
 * @Date: 2020/10/14 14:42
 */
@Repository
public class OperRoleDaoImpl implements OperRoleDao{

    /**
     * 查询操作员角色列表
     * @param operId
     * @return
     */
    public List<SysOperRole> listByOperId(Long operId) {
        StringBuffer sql = new StringBuffer();
        sql.append(" select t1.*, ")
                .append(" (select role_name from sys_role t2 where t2.role_id = t1.role_id and t2.state='1') as role_name")
                .append(" from sys_oper_role t1 where oper_id = ?");

        return SysOperRole.dao.find(sql.toString(), operId);
    }

    /**
     * 修改账号
     * @param operId
     * @param roleId
     */
    public void save(Long operId, Long roleId,Long sysOperId) {
        SysOperRole operRole = new SysOperRole();
        operRole.setOperId(operId);
        operRole.setRoleId(roleId);
        operRole.setCreateOperId(sysOperId);
        operRole.setCreateTime(new Date());
        operRole.save();
    }

    /**
     * 根据账号编码查询账号信息
     * @param orId
     * @return
     */
    public void delete(Long orId) {
        SysOperRole.dao.deleteById(orId);
    }

    /**
     * 根据账号编码删除账号角色
     * @param operId
     * @return
     */
    public void deleteByOperId(Long operId) {
        StringBuffer sql = new StringBuffer();
        sql.append("delete from sys_oper_role where oper_id = ?");
        Db.delete(sql.toString(), operId);
    }

    /**
     * 查询操作员角色列表
     * @param roleId
     * @return
     */
    public List<SysOper> listOperByRoleId(Long roleId) {
        StringBuffer sql = new StringBuffer();
        sql.append(" select t2.* ")
                .append(" from sys_oper_role t1 left join sys_oper t2 on t2.oper_id = t1.oper_id")
                .append(" where role_id = ? and t2.status = '1' and t2.state='1' ");

        return SysOper.dao.find(sql.toString(), roleId);
    }
}
